package com.logic.modular.game.player.info.service;

import com.comment.dubbo.service.PlayerService;
import com.comment.exception.ServiceException;
import com.comment.util.message.MapUtil;
import com.logic.comment.util.JwtUtils;
import com.logic.modular.game.player.bag.service.BagItemServiceImpl;
import com.logic.modular.game.player.bag.service.BagServiceImpl;
import com.logic.modular.game.player.hero.service.PlayerHeroServiceImpl;
import com.logic.modular.game.player.info.PlayerBaseServiceImpl;
import com.logic.modular.game.player.info.PlayerRankServiceImpl;
import com.logic.modular.game.player.info.mapper.PlayerMapper;
import com.logic.modular.game.player.science.PlayerScienceService;
import com.comment.pojo.domain.logic.domain.Player;
import com.logic.modular.game.player.skill.PlayerSkillServiceImpl;
import com.logic.modular.game.player.match.service.PvpRanksServiceImpl;
import com.logic.modular.game.player.task.service.PlayerTaskServiceImpl;
import com.logic.modular.game.statistical.service.impl.PlayerStatisticalPoolServiceImpl;
import com.logic.modular.gameCore.config.service.impl.CoreConfigServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 游戏账户(Player)表服务实现类
 *
 * @author cxy
 * @since 2024-07-18 10:05:14
 */
@Slf4j
@Service
@DubboService(interfaceClass = PlayerService.class)
public class PlayerServiceImpl extends PlayerBaseServiceImpl
        implements PlayerService {
    @Value("${secret.key}")
    private String key;
    @Resource
    private PlayerHeroServiceImpl playerHeroService;
    @Resource
    private PvpRanksServiceImpl pvpRanksService;
    @Resource
    private PlayerSkillServiceImpl playerSkillService;
    @Resource
    private PlayerRankServiceImpl playerRankServiceImpl;
    @Resource
    private PlayerTaskServiceImpl playerTaskService;
    @Resource
    private PlayerStatisticalPoolServiceImpl playerStatisticalPoolService;
    @Resource
    private BagServiceImpl bagServiceImpl;
    @Resource
    private PlayerScienceService playerScienceServiceImpl;
    @Resource
    private PlayerMapper playerMapper;
    @Resource
    private CoreConfigServiceImpl battleConfigServiceImpl;

    private Player init(Long playerId) {
        Player account = Player.builder().uid(playerId).build();
        this.save(account);
        bagServiceImpl.registerInit(playerId);// 赠送服务
        this.playerHeroService.registerInit(playerId);
        this.playerSkillService.registerInit(playerId);
        this.playerRankServiceImpl.init(playerId);
        return this.selectCache(playerId);
    }

    public void init(Player player) {
        Long playerId = player.getUid();
        this.save(player);
        bagServiceImpl.registerInit(playerId);// 赠送服务
        this.playerHeroService.registerInit(playerId);
        this.playerSkillService.registerInit(playerId);
        this.playerRankServiceImpl.init(playerId);
    }

    @Transactional
    public String login(Long playerId, String serveKey) {
        if (!serveKey.equals(this.key)) {
            throw new ServiceException("非法访问");
        } else {
            String jwtToken = JwtUtils.getToken(playerId);
            if (jwtToken != null) {
                String jwtToken1 = JwtUtils.createJwtToken(playerId + "," + serveKey);

                assert jwtToken1 != null;

                if (jwtToken1.equals(jwtToken)) {
                    return jwtToken;
                }
            }
            Player player = select(playerId);
            String token;
            if (player == null) {
                player = this.init(playerId);
                token = JwtUtils.updateJwtToken(player);
            } else {
                player.setLastLoginTime(new Date());
                this.updateById(player);
                token = JwtUtils.updateJwtToken(player);
                playerScienceServiceImpl.obtainPlayerScience(playerId);
            }

            return token;
        }
    }

    public List<Player> obtainFreeRobot(int num) {
        List<Player> players = playerMapper.obtainFreeRobot(num);
        if (players.size() < num) {
            players = playerMapper.obtainFreeRobot(num);
            if (players.isEmpty()) {
                log.error("机器人不足");
            }
        }
        return players;
    }

    @Override
    public Set<Long> obtainPlayerIds() {
        return playerMapper.obtainPlayerIds();
    }

    @Resource
    private BagItemServiceImpl bagItemService;

    public boolean rename(Long playerId, String nickname) {
        boolean b = false;
        String s = bagItemService.pledgeProp(playerId, MapUtil.of(1L, BigDecimal.valueOf(500)), "rename");
        try {
            Player byId = getById(playerId);
            byId.setNickname(nickname);
            b = updateById(byId);
        } catch (Exception e) {
            log.error("重命名失败", e);
            bagItemService.redemptionOfProp(s);
            return b;
        }
        bagItemService.abandoningPledge(s);
        return b;
    }
}
